Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n-api: delete callback bundle via reference #29479

Conversation

gabrielschulhof
Copy link
Contributor

We should strive to use weak persistent references consistently
throughout the code, since using v8::Persistent directly results
in having to change many sites in the code when the way we use it
changes.

N-API uses v8impl::Reference internally when maintaining a weak
persistent reference is necessary. So far, v8impl::CallbackBundle was
using v8::Persistent directly in order to weakly reference the JS
function backed by a N-API callback.

The change introduced here reduces v8impl::CallbackBundle to a simple
structure and uses a v8impl::Reference to weakly reference the N-API
callback with which it is associated. The structure is freed by the
napi_finalize callback of the v8impl::Reference. This brings
N-API use of v8::Persistent completely under the v8impl::Reference
umbrella, rendering our use of weak references consistent.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

Benchmark comparison:

Registered S3 methods overwritten by 'ggplot2':
  method         from 
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang
                                                              confidence improvement accuracy (*)    (**)   (***)
 napi/function_args n=1 engine='napi' type='1000Numbers'                      0.49 %       ±5.22%  ±6.95%  ±9.04%
 napi/function_args n=1 engine='napi' type='100Numbers'                      -1.16 %       ±6.24%  ±8.31% ±10.82%
 napi/function_args n=1 engine='napi' type='10Numbers'                       -0.34 %       ±8.35% ±11.11% ±14.47%
 napi/function_args n=1 engine='napi' type='Array'                           -1.01 %       ±7.86% ±10.47% ±13.64%
 napi/function_args n=1 engine='napi' type='Number'                          -6.30 %       ±7.68% ±10.22% ±13.31%
 napi/function_args n=1 engine='napi' type='Object'                          -2.60 %       ±7.47%  ±9.97% ±13.03%
 napi/function_args n=1 engine='napi' type='String'                          -1.36 %       ±7.40%  ±9.84% ±12.82%
 napi/function_args n=1 engine='napi' type='Typedarray'                       2.70 %       ±8.15% ±10.86% ±14.14%
 napi/function_args n=1 engine='v8' type='1000Numbers'                       -5.25 %       ±7.04%  ±9.39% ±12.27%
 napi/function_args n=1 engine='v8' type='100Numbers'                         5.47 %       ±7.27%  ±9.69% ±12.66%
 napi/function_args n=1 engine='v8' type='10Numbers'                          1.23 %       ±7.49%  ±9.97% ±12.98%
 napi/function_args n=1 engine='v8' type='Array'                             -5.20 %       ±8.10% ±10.77% ±14.02%
 napi/function_args n=1 engine='v8' type='Number'                            -4.34 %       ±7.72% ±10.27% ±13.38%
 napi/function_args n=1 engine='v8' type='Object'                      *     -9.54 %       ±7.34%  ±9.78% ±12.74%
 napi/function_args n=1 engine='v8' type='String'                            -6.10 %       ±8.93% ±11.88% ±15.48%
 napi/function_args n=1 engine='v8' type='Typedarray'                         1.93 %       ±7.43%  ±9.89% ±12.88%
 napi/function_args n=10 engine='napi' type='1000Numbers'                     5.32 %       ±9.21% ±12.26% ±15.99%
 napi/function_args n=10 engine='napi' type='100Numbers'                     -0.73 %       ±8.35% ±11.11% ±14.47%
 napi/function_args n=10 engine='napi' type='10Numbers'                       3.43 %       ±7.39%  ±9.86% ±12.87%
 napi/function_args n=10 engine='napi' type='Array'                           0.07 %       ±9.23% ±12.28% ±16.00%
 napi/function_args n=10 engine='napi' type='Number'                          3.94 %       ±8.79% ±11.69% ±15.22%
 napi/function_args n=10 engine='napi' type='Object'                         -0.75 %       ±6.61%  ±8.79% ±11.44%
 napi/function_args n=10 engine='napi' type='String'                         -1.84 %       ±7.45%  ±9.92% ±12.92%
 napi/function_args n=10 engine='napi' type='Typedarray'                     -3.52 %       ±7.50%  ±9.98% ±12.99%
 napi/function_args n=10 engine='v8' type='1000Numbers'                      -2.69 %       ±7.97% ±10.61% ±13.81%
 napi/function_args n=10 engine='v8' type='100Numbers'                        4.32 %       ±7.86% ±10.46% ±13.64%
 napi/function_args n=10 engine='v8' type='10Numbers'                         0.47 %       ±7.51% ±10.00% ±13.03%
 napi/function_args n=10 engine='v8' type='Array'                             0.69 %       ±7.54% ±10.03% ±13.06%
 napi/function_args n=10 engine='v8' type='Number'                           -6.67 %       ±9.49% ±12.65% ±16.49%
 napi/function_args n=10 engine='v8' type='Object'                           -4.90 %       ±8.97% ±11.94% ±15.56%
 napi/function_args n=10 engine='v8' type='String'                            2.29 %       ±9.35% ±12.45% ±16.22%
 napi/function_args n=10 engine='v8' type='Typedarray'                       -0.83 %       ±6.54%  ±8.70% ±11.33%
 napi/function_args n=100 engine='napi' type='1000Numbers'                   -1.30 %       ±3.48%  ±4.65%  ±6.09%
 napi/function_args n=100 engine='napi' type='100Numbers'                     1.13 %       ±8.47% ±11.27% ±14.68%
 napi/function_args n=100 engine='napi' type='10Numbers'                     -3.54 %       ±7.30%  ±9.72% ±12.65%
 napi/function_args n=100 engine='napi' type='Array'                         -0.21 %       ±4.08%  ±5.47%  ±7.19%
 napi/function_args n=100 engine='napi' type='Number'                         2.08 %       ±8.19% ±10.89% ±14.18%
 napi/function_args n=100 engine='napi' type='Object'                         0.75 %       ±8.52% ±11.33% ±14.75%
 napi/function_args n=100 engine='napi' type='String'                         3.96 %       ±7.48%  ±9.95% ±12.96%
 napi/function_args n=100 engine='napi' type='Typedarray'                    -1.04 %       ±7.97% ±10.61% ±13.82%
 napi/function_args n=100 engine='v8' type='1000Numbers'                     -1.58 %       ±4.84%  ±6.44%  ±8.39%
 napi/function_args n=100 engine='v8' type='100Numbers'                      -3.11 %       ±5.94%  ±7.90% ±10.29%
 napi/function_args n=100 engine='v8' type='10Numbers'                       -0.25 %       ±6.99%  ±9.30% ±12.11%
 napi/function_args n=100 engine='v8' type='Array'                            1.70 %       ±3.74%  ±5.01%  ±6.57%
 napi/function_args n=100 engine='v8' type='Number'                           8.56 %       ±9.45% ±12.58% ±16.37%
 napi/function_args n=100 engine='v8' type='Object'                          -0.25 %       ±6.60%  ±8.78% ±11.42%
 napi/function_args n=100 engine='v8' type='String'                           1.73 %       ±6.59%  ±8.78% ±11.43%
 napi/function_args n=100 engine='v8' type='Typedarray'                      -1.87 %       ±6.45%  ±8.58% ±11.16%
 napi/function_args n=1000 engine='napi' type='1000Numbers'                  -2.26 %       ±3.93%  ±5.23%  ±6.82%
 napi/function_args n=1000 engine='napi' type='100Numbers'                   -0.51 %       ±4.08%  ±5.44%  ±7.11%
 napi/function_args n=1000 engine='napi' type='10Numbers'                     1.93 %       ±9.13% ±12.15% ±15.82%
 napi/function_args n=1000 engine='napi' type='Array'                         3.08 %       ±3.50%  ±4.65%  ±6.06%
 napi/function_args n=1000 engine='napi' type='Number'                       -0.86 %       ±6.97%  ±9.28% ±12.09%
 napi/function_args n=1000 engine='napi' type='Object'                        0.78 %       ±3.73%  ±4.99%  ±6.52%
 napi/function_args n=1000 engine='napi' type='String'                       -3.01 %       ±6.87%  ±9.17% ±11.97%
 napi/function_args n=1000 engine='napi' type='Typedarray'                   -1.80 %       ±6.20%  ±8.25% ±10.74%
 napi/function_args n=1000 engine='v8' type='1000Numbers'              *     -4.82 %       ±4.07%  ±5.45%  ±7.16%
 napi/function_args n=1000 engine='v8' type='100Numbers'                     -1.47 %       ±4.63%  ±6.17%  ±8.03%
 napi/function_args n=1000 engine='v8' type='10Numbers'                      -0.75 %       ±6.83%  ±9.10% ±11.86%
 napi/function_args n=1000 engine='v8' type='Array'                           0.42 %       ±4.96%  ±6.60%  ±8.61%
 napi/function_args n=1000 engine='v8' type='Number'                          3.97 %       ±7.21%  ±9.60% ±12.52%
 napi/function_args n=1000 engine='v8' type='Object'                          0.41 %       ±2.32%  ±3.09%  ±4.02%
 napi/function_args n=1000 engine='v8' type='String'                          1.51 %       ±7.28%  ±9.69% ±12.62%
 napi/function_args n=1000 engine='v8' type='Typedarray'                     -1.20 %       ±6.81%  ±9.06% ±11.80%
 napi/function_args n=10000 engine='napi' type='1000Numbers'                 -0.13 %       ±4.39%  ±5.84%  ±7.60%
 napi/function_args n=10000 engine='napi' type='100Numbers'            *      5.83 %       ±5.23%  ±7.01%  ±9.24%
 napi/function_args n=10000 engine='napi' type='10Numbers'                    0.83 %       ±4.67%  ±6.24%  ±8.17%
 napi/function_args n=10000 engine='napi' type='Array'                        2.01 %       ±3.97%  ±5.28%  ±6.87%
 napi/function_args n=10000 engine='napi' type='Number'                      -1.35 %       ±2.42%  ±3.23%  ±4.22%
 napi/function_args n=10000 engine='napi' type='Object'                *     -3.31 %       ±2.83%  ±3.78%  ±4.95%
 napi/function_args n=10000 engine='napi' type='String'                       0.50 %       ±3.02%  ±4.02%  ±5.23%
 napi/function_args n=10000 engine='napi' type='Typedarray'                  -1.59 %       ±4.69%  ±6.24%  ±8.12%
 napi/function_args n=10000 engine='v8' type='1000Numbers'                   -1.86 %       ±4.62%  ±6.16%  ±8.05%
 napi/function_args n=10000 engine='v8' type='100Numbers'            ***     -4.76 %       ±2.27%  ±3.03%  ±3.94%
 napi/function_args n=10000 engine='v8' type='10Numbers'                     -4.10 %       ±6.11%  ±8.18% ±10.76%
 napi/function_args n=10000 engine='v8' type='Array'                         -3.02 %       ±3.77%  ±5.03%  ±6.59%
 napi/function_args n=10000 engine='v8' type='Number'                        -3.69 %       ±5.16%  ±6.87%  ±8.96%
 napi/function_args n=10000 engine='v8' type='Object'                        -1.77 %       ±2.84%  ±3.80%  ±4.98%
 napi/function_args n=10000 engine='v8' type='String'                         0.87 %       ±2.80%  ±3.72%  ±4.85%
 napi/function_args n=10000 engine='v8' type='Typedarray'                     1.39 %       ±5.30%  ±7.11%  ±9.38%
 napi/function_args n=100000 engine='napi' type='1000Numbers'                 0.17 %       ±2.80%  ±3.72%  ±4.84%
 napi/function_args n=100000 engine='napi' type='100Numbers'                 -0.19 %       ±2.12%  ±2.82%  ±3.67%
 napi/function_args n=100000 engine='napi' type='10Numbers'                  -0.30 %       ±0.64%  ±0.86%  ±1.12%
 napi/function_args n=100000 engine='napi' type='Array'                       1.54 %       ±2.57%  ±3.41%  ±4.45%
 napi/function_args n=100000 engine='napi' type='Number'                      0.05 %       ±0.35%  ±0.46%  ±0.61%
 napi/function_args n=100000 engine='napi' type='Object'                      1.01 %       ±1.51%  ±2.00%  ±2.61%
 napi/function_args n=100000 engine='napi' type='String'                      0.05 %       ±0.37%  ±0.49%  ±0.63%
 napi/function_args n=100000 engine='napi' type='Typedarray'                 -0.11 %       ±0.30%  ±0.40%  ±0.52%
 napi/function_args n=100000 engine='v8' type='1000Numbers'          ***     -6.30 %       ±2.77%  ±3.68%  ±4.79%
 napi/function_args n=100000 engine='v8' type='100Numbers'                   -1.20 %       ±1.38%  ±1.85%  ±2.42%
 napi/function_args n=100000 engine='v8' type='10Numbers'                     0.27 %       ±0.85%  ±1.13%  ±1.48%
 napi/function_args n=100000 engine='v8' type='Array'                         1.58 %       ±3.02%  ±4.01%  ±5.23%
 napi/function_args n=100000 engine='v8' type='Number'                       -0.03 %       ±0.22%  ±0.29%  ±0.37%
 napi/function_args n=100000 engine='v8' type='Object'                       -0.34 %       ±1.55%  ±2.07%  ±2.69%
 napi/function_args n=100000 engine='v8' type='String'                        0.34 %       ±0.42%  ±0.56%  ±0.73%
 napi/function_args n=100000 engine='v8' type='Typedarray'                   -0.06 %       ±0.17%  ±0.23%  ±0.29%
 napi/function_call n=1000000 type='cxx'                                      0.17 %       ±0.25%  ±0.33%  ±0.43%
 napi/function_call n=1000000 type='js'                                      -0.00 %       ±0.09%  ±0.12%  ±0.15%
 napi/function_call n=1000000 type='napi'                                     0.18 %       ±0.47%  ±0.63%  ±0.82%
 napi/function_call n=10000000 type='cxx'                                    -1.06 %       ±2.11%  ±2.81%  ±3.66%
 napi/function_call n=10000000 type='js'                                     -0.03 %       ±0.15%  ±0.20%  ±0.26%
 napi/function_call n=10000000 type='napi'                                    1.20 %       ±2.36%  ±3.14%  ±4.09%
 napi/function_call n=50000000 type='cxx'                                    -1.07 %       ±3.07%  ±4.10%  ±5.35%
 napi/function_call n=50000000 type='js'                                     -0.36 %       ±0.87%  ±1.16%  ±1.51%
 napi/function_call n=50000000 type='napi'                                   -0.07 %       ±3.52%  ±4.68%  ±6.10%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case there are 105 comparisons, you can thus
expect the following amount of false-positive results:
  5.25 false positives, when considering a   5% risk acceptance (*, **, ***),
  1.05 false positives, when considering a   1% risk acceptance (**, ***),
  0.10 false positives, when considering a 0.1% risk acceptance (***)

I think napi/function_call is the most relevant.

We should strive to use weak persistent references consistently
throughout the code, since using `v8::Persistent` directly results
in having to change many sites in the code when the way we use it
changes.

N-API uses `v8impl::Reference` internally when maintaining a weak
persistent reference is necessary. So far, `v8impl::CallbackBundle` was
using `v8::Persistent` directly in order to weakly reference the JS
function backed by a N-API callback.

The change introduced here reduces `v8impl::CallbackBundle` to a simple
structure and uses a `v8impl::Reference` to weakly reference the N-API
callback with which it is associated. The structure is freed by the
`napi_finalize` callback of the `v8impl::Reference`. This brings
N-API use of `v8::Persistent` completely under the `v8impl::Reference`
umbrella, rendering our use of weak references consistent.
@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Sep 6, 2019
@gabrielschulhof gabrielschulhof added the node-api Issues and PRs related to the Node-API. label Sep 6, 2019
Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confidence level of some of those benchmarks that cannot possibly be affected by this change doesn't inspire, ah, much confidence in me. ^^

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

gabrielschulhof pushed a commit that referenced this pull request Sep 9, 2019
We should strive to use weak persistent references consistently
throughout the code, since using `v8::Persistent` directly results
in having to change many sites in the code when the way we use it
changes.

N-API uses `v8impl::Reference` internally when maintaining a weak
persistent reference is necessary. So far, `v8impl::CallbackBundle` was
using `v8::Persistent` directly in order to weakly reference the JS
function backed by a N-API callback.

The change introduced here reduces `v8impl::CallbackBundle` to a simple
structure and uses a `v8impl::Reference` to weakly reference the N-API
callback with which it is associated. The structure is freed by the
`napi_finalize` callback of the `v8impl::Reference`. This brings
N-API use of `v8::Persistent` completely under the `v8impl::Reference`
umbrella, rendering our use of weak references consistent.

PR-URL: #29479
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@gabrielschulhof
Copy link
Contributor Author

Landed in 1f4f2b4.

targos pushed a commit that referenced this pull request Sep 20, 2019
We should strive to use weak persistent references consistently
throughout the code, since using `v8::Persistent` directly results
in having to change many sites in the code when the way we use it
changes.

N-API uses `v8impl::Reference` internally when maintaining a weak
persistent reference is necessary. So far, `v8impl::CallbackBundle` was
using `v8::Persistent` directly in order to weakly reference the JS
function backed by a N-API callback.

The change introduced here reduces `v8impl::CallbackBundle` to a simple
structure and uses a `v8impl::Reference` to weakly reference the N-API
callback with which it is associated. The structure is freed by the
`napi_finalize` callback of the `v8impl::Reference`. This brings
N-API use of `v8::Persistent` completely under the `v8impl::Reference`
umbrella, rendering our use of weak references consistent.

PR-URL: #29479
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@BridgeAR BridgeAR mentioned this pull request Sep 24, 2019
BridgeAR pushed a commit that referenced this pull request Sep 25, 2019
We should strive to use weak persistent references consistently
throughout the code, since using `v8::Persistent` directly results
in having to change many sites in the code when the way we use it
changes.

N-API uses `v8impl::Reference` internally when maintaining a weak
persistent reference is necessary. So far, `v8impl::CallbackBundle` was
using `v8::Persistent` directly in order to weakly reference the JS
function backed by a N-API callback.

The change introduced here reduces `v8impl::CallbackBundle` to a simple
structure and uses a `v8impl::Reference` to weakly reference the N-API
callback with which it is associated. The structure is freed by the
`napi_finalize` callback of the `v8impl::Reference`. This brings
N-API use of `v8::Persistent` completely under the `v8impl::Reference`
umbrella, rendering our use of weak references consistent.

PR-URL: #29479
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@gabrielschulhof gabrielschulhof deleted the napi-use-reference-for-callback-bundle branch October 27, 2019 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants